<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { getBorrowerListService, delBorrowerService } from '@/api/borrower.js'
import BrrowersEditAdd from './components/brrowersEditAdd.vue'
import { ElInput } from 'element-plus'
const borrowerList = ref([])
const loading = ref(false)
const dialog = ref()
const queryinf = ref('')
const isQuery = ref(false)

const getBorrowerlList = async () => {
  isQuery.value = true
  loading.value = true
  const res = await getBorrowerListService(queryinf.value)
  borrowerList.value = res.data.data
  loading.value = false
}
const onDelBorrower = async (row) => {
  await ElMessageBox.confirm('你确认要删除该读者用户么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await delBorrowerService(row.username)
  ElMessage.success('删除成功')
  getBorrowerlList()
}
const onEditBorrower = (row) => {
  dialog.value.open(row)
}
const onAddBorrower = () => {
  dialog.value.open({})
}
const onSuccess = () => {
  getBorrowerlList()
}
</script>

<template>
  <page-container title="读者管理">
    <template #extra>
      <ElInput v-model="queryinf" clearable style="width: 200px"></ElInput>
      <el-button @click="getBorrowerlList">查询读者</el-button>
      <el-button @click="onAddBorrower">添加读者</el-button>
    </template>

    <el-table v-loading="loading" :data="borrowerList" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="nickName" label="昵称"></el-table-column>
      <el-table-column label="操作" width="150">
        <!-- row 就是 channelList 的一项， $index 下标 -->
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditBorrower(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelBorrower(row, $index)"
          ></el-button>
        </template>
      </el-table-column>

      <template #empty>
        <el-empty description="没有数据" v-if="isQuery === true"></el-empty>
        <el-empty
          description="请输入用户名或者昵称的关键字查询读者"
          v-if="isQuery === false"
        ></el-empty>
      </template>
    </el-table>

    <brrowers-edit-add ref="dialog" @success="onSuccess"></brrowers-edit-add>
  </page-container>
</template>

<style lang="scss" scoped></style>
